<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="合同编号" prop="htid">
        <el-input
          v-model="queryParams.htid"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="htmc">
        <el-input
          v-model="queryParams.htmc"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同类型" prop="htlx">
        <el-select
          v-model="queryParams.htlx"
          placeholder="请选择合同类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_HTLX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同类别" prop="htlb">
        <el-select
          v-model="queryParams.htlb"
          placeholder="请选择合同类别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_HTLB)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务开始时间" prop="kssj">
        <el-date-picker
          v-model="queryParams.kssj"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="date"
          placeholder="选择服务开始时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="服务结束时间" prop="jssj">
        <el-date-picker
          v-model="queryParams.jssj"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="选择服务结束时间"
          end-placeholder="选择服务结束时间"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="付款方式" prop="fkfs">
        <el-select
          v-model="queryParams.fkfs"
          placeholder="请选择付款方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_FKFS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户单位" prop="khdw">
        <el-input
          v-model="queryParams.khdw"
          placeholder="请输入客户单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户联系人" prop="khlxr">
        <el-input
          v-model="queryParams.khlxr"
          placeholder="请输入客户联系人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同状态" prop="htzt">
        <el-select
          v-model="queryParams.htzt"
          placeholder="请选择合同状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_HTZT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="签订时间" prop="qdsj">
        <el-date-picker
          v-model="queryParams.qdsj"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="date"
          placeholder="选择签订时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="线号" prop="xid">
        <el-input
          v-model="queryParams.xid"
          placeholder="请输入线号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="A端地址" prop="jrdd">
        <el-input
          v-model="queryParams.jrdd"
          placeholder="请输入A端地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="Z端地址" prop="scdd">
        <el-input
          v-model="queryParams.scdd"
          placeholder="请输入Z端地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cont:base-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cont:base-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="合同编号" align="center" prop="htid" />
      <el-table-column label="合同名称" align="center" prop="htmc" />
      <el-table-column label="合同类型" align="center" prop="htlx">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONT_HTLX" :value="scope.row.htlx" />
        </template>
      </el-table-column>
      <el-table-column label="合同类别" align="center" prop="htlb">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONT_HTLB" :value="scope.row.htlb" />
        </template>
      </el-table-column>
      <el-table-column label="二维码/条形码" align="center" prop="ewm">
        <template #default="scope">
          <img v-if="scope.row.ewm" :src="scope.row.ewm" class="h-30px" @click="imagePreview(scope.row.ewm)"/>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="htje" />
      <el-table-column
        label="服务开始时间"
        align="center"
        prop="kssj"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="服务结束时间"
        align="center"
        prop="jssj"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="项目名称" align="center" prop="xmmc" />
      <el-table-column label="部门" align="center" prop="bm" />
      <el-table-column label="负责人" align="center" prop="fzr" />
      <el-table-column label="合同执行方式" align="center" prop="zxfs" />
      <el-table-column label="合同主要内容" align="center" prop="zynr" />
      <el-table-column label="付款方式" align="center" prop="fkfs">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONT_FKFS" :value="scope.row.fkfs" />
        </template>
      </el-table-column>
      <el-table-column label="客户单位" align="center" prop="khdw" />
      <el-table-column label="客户联系人" align="center" prop="khlxr" />
      <el-table-column label="合同状态" align="center" prop="htzt">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONT_HTZT" :value="scope.row.htzt" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="bz" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="签订时间"
        align="center"
        prop="qdsj"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="线号" align="center" prop="xid" />
      <el-table-column label="A端地址" align="center" prop="jrdd" />
      <el-table-column label="Z端地址" align="center" prop="scdd" />
      <el-table-column label="操作" align="center" fixed="right" width="180">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cont:base-info:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cont:base-info:delete']"
          >
            删除
          </el-button>
          <el-button
            link
            type="warning"
            @click="openForm('check', scope.row.id)"
            v-hasPermi="['cont:base-info:check']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <BaseInfoForm ref="formRef" @success="getList" />
  <!-- 子表的列表 -->
  <ContentWrap>
    <el-tabs model-value="detailInfo">
      <el-tab-pane label="建设合同详细信息" name="detailInfo">
        <DetailInfoList :htid="currentRow.id" />
      </el-tab-pane>
      <el-tab-pane label="合同资料表 " name="materialInfo">
        <MaterialInfoList :htid="currentRow.id" />
      </el-tab-pane>
      <el-tab-pane label="合同收付款记录表 " name="payInfo">
        <PayInfoList :htid="currentRow.id" />
      </el-tab-pane>
      <el-tab-pane label="合同变更记录表 " name="changeInfo">
        <ChangeInfoList :htid="currentRow.id" />
      </el-tab-pane>
      <el-tab-pane label="合同收付款计划表 " name="planInfo">
        <PlanInfoList :htid="currentRow.id" />
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as BaseInfoApi from '@/api/cont/base'
import BaseInfoForm from './BaseInfoForm.vue'
import DetailInfoList from './components/DetailInfoList.vue'
import MaterialInfoList from './components/MaterialInfoList.vue'
import PayInfoList from './components/PayInfoList.vue'
import ChangeInfoList from './components/ChangeInfoList.vue'
import PlanInfoList from './components/PlanInfoList.vue'
import { createImageViewer } from '@/components/ImageViewer'

defineOptions({ name: 'BaseInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  htid: undefined,
  htmc: undefined,
  htlx: undefined,
  htlb: undefined,
  kssj: undefined,
  kssj: [],
  jssj: undefined,
  jssj: [],
  fkfs: undefined,
  khdw: undefined,
  khlxr: undefined,
  htzt: undefined,
  createTime: [],
  qdsj: undefined,
  qdsj: [],
  xid: undefined,
  jrdd: undefined,
  scdd: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BaseInfoApi.getBaseInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BaseInfoApi.deleteBaseInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await BaseInfoApi.exportBaseInfo(queryParams)
    download.excel(data, '合同基础信息表 .xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 图片预览 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>